/**
 * created by 小卷毛, 2020/10/21
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.mdrh.task.util

import com.sgcc.pda.bluetooth.utils.ToolGson
import com.sgcc.pda.hzaidl.entity.SafeCallback
import com.sgcc.pda.mdrh.task.common_ui.helper.optNotEmpty
import com.sgcc.pda.mdrh.task.model.PlanData
import com.sgcc.pda.safe.impl.SafeManager
import org.json.JSONObject

/**
 * 描 述：
 * 作 者：小卷毛  2020-10-21 14:16
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */

fun SafeManager.getPlanList(
    onSuccess: (ArrayList<PlanData>) -> Unit = {},
    onError: (String) -> Unit = {},
    onFinally: () -> Unit = {}
) {
    getMonitorPlanList(
        object : SafeCallback {
            override fun onSuccess(data: String) {
                val json = JSONObject(data)
                val msgCode = json.optString("RT_F", "")

                if (msgCode == "1") {
                    onSuccess(
                        ToolGson.getBeans(
                            json.optNotEmpty("DTS", "[]"),
                            PlanData::class.java
                        ) as ArrayList<PlanData>
                    )
                    onFinally()
                } else {
                    onError(
                        json.optNotEmpty(
                            "RT_D",
                            "网络数据请求失败，请联系管理员！"
                        )
                    )
                    onFinally()
                }
            }

            override fun onFail(code: String?, message: String?) {
                onError("网络数据请求失败！")
                onFinally()
            }

            override fun getTag() = ""
        }
    )
}